<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮胎管理系统 - 登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 100%;
            max-width: 400px;
        }
        .login-header {
            background: linear-gradient(135deg, #409eff 0%, #67c23a 100%);
            color: white;
            padding: 30px 20px;
            text-align: center;
        }
        .login-header h2 {
            margin: 0;
            font-size: 24px;
            font-weight: 600;
        }
        .login-header p {
            margin: 10px 0 0 0;
            opacity: 0.9;
        }
        .login-form {
            padding: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-control {
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            transition: all 0.3s;
        }
        .form-control:focus {
            border-color: #409eff;
            box-shadow: 0 0 0 0.2rem rgba(64, 158, 255, 0.25);
        }
        .input-group-text {
            background: #f8f9fa;
            border: 2px solid #e9ecef;
            border-right: none;
        }
        .form-control.with-icon {
            border-left: none;
        }
        .btn-login {
            background: linear-gradient(135deg, #409eff 0%, #67c23a 100%);
            border: none;
            color: white;
            padding: 12px;
            border-radius: 8px;
            font-weight: 600;
            width: 100%;
            transition: all 0.3s;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        .btn-login:disabled {
            opacity: 0.6;
            transform: none;
        }
        .device-type-group {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        .device-type-btn {
            flex: 1;
            padding: 10px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            background: white;
            cursor: pointer;
            transition: all 0.3s;
            text-align: center;
        }
        .device-type-btn.active {
            border-color: #409eff;
            background: #f0f7ff;
            color: #409eff;
        }
        .alert {
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .spinner-border-sm {
            width: 1rem;
            height: 1rem;
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h2>轮胎管理系统</h2>
        <p>欢迎登录</p>
    </div>

    <div class="login-form">
        <div id="errorAlert" class="alert alert-danger d-none" role="alert"></div>
        <div id="successAlert" class="alert alert-success d-none" role="alert"></div>

        <form id="loginForm">
            <div class="form-group">
                <div class="input-group">
                        <span class="input-group-text">
                            <i class="fas fa-user"></i>
                        </span>
                    <input type="text" class="form-control with-icon" id="username"
                           placeholder="请输入用户名" required>
                </div>
                <div class="invalid-feedback" id="usernameError"></div>
            </div>

            <div class="form-group">
                <div class="input-group">
                        <span class="input-group-text">
                            <i class="fas fa-lock"></i>
                        </span>
                    <input type="password" class="form-control with-icon" id="password"
                           placeholder="请输入密码" required>
                    <button class="btn btn-outline-secondary" type="button" id="togglePassword">
                        <i class="fas fa-eye"></i>
                    </button>
                </div>
                <div class="invalid-feedback" id="passwordError"></div>
            </div>

            <div class="form-group">
                <label class="form-label">设备类型</label>
                <div class="device-type-group">
                    <div class="device-type-btn active" data-value="WEB">
                        <i class="fas fa-desktop"></i><br>
                        <span>网页端</span>
                    </div>
                    <div class="device-type-btn" data-value="ANDROID">
                        <i class="fas fa-mobile-alt"></i><br>
                        <span>安卓端</span>
                    </div>
                </div>
                <input type="hidden" id="deviceType" value="WEB">
            </div>

            <button type="submit" class="btn btn-login" id="loginBtn">
                <span id="loginText">登录</span>
                <span id="loginSpinner" class="spinner-border spinner-border-sm d-none" role="status"></span>
            </button>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const loginForm = document.getElementById('loginForm');
        const usernameInput = document.getElementById('username');
        const passwordInput = document.getElementById('password');
        const deviceTypeInput = document.getElementById('deviceType');
        const loginBtn = document.getElementById('loginBtn');
        const loginText = document.getElementById('loginText');
        const loginSpinner = document.getElementById('loginSpinner');
        const errorAlert = document.getElementById('errorAlert');
        const successAlert = document.getElementById('successAlert');
        const togglePasswordBtn = document.getElementById('togglePassword');

        // 设备类型选择
        document.querySelectorAll('.device-type-btn').forEach(btn => {
            btn.addEventListener('click', function() {
                document.querySelectorAll('.device-type-btn').forEach(b => b.classList.remove('active'));
                this.classList.add('active');
                deviceTypeInput.value = this.getAttribute('data-value');
            });
        });

        // 显示/隐藏密码
        togglePasswordBtn.addEventListener('click', function() {
            const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
            passwordInput.setAttribute('type', type);
            this.innerHTML = type === 'password' ? '<i class="fas fa-eye"></i>' : '<i class="fas fa-eye-slash"></i>';
        });

        // 表单提交
        loginForm.addEventListener('submit', async function(e) {
            e.preventDefault();

            // 验证表单
            if (!validateForm()) {
                return;
            }

            // 显示加载状态
            setLoading(true);
            hideAlerts();

            try {
                const loginData = {
                    username: usernameInput.value.trim(),
                    password: passwordInput.value,
                    deviceType: deviceTypeInput.value
                };

                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(loginData)
                });

                const result = await response.json();

                if (result.success) {
                    showAlert(successAlert, '登录成功！正在跳转...');

                    // 保存token和用户信息到localStorage
                    localStorage.setItem('token', result.token);
                    localStorage.setItem('userInfo', JSON.stringify(result.userInfo));

                    // 跳转到首页
                    setTimeout(() => {
                        window.location.href = '/dashboard';
                    }, 1000);
                } else {
                    showAlert(errorAlert, result.message || '登录失败');
                }
            } catch (error) {
                console.error('Login error:', error);
                showAlert(errorAlert, '登录失败，请检查网络连接');
            } finally {
                setLoading(false);
            }
        });

        function validateForm() {
            let isValid = true;
            hideAlerts();

            // 验证用户名
            if (!usernameInput.value.trim()) {
                showFieldError(usernameInput, '请输入用户名');
                isValid = false;
            } else {
                clearFieldError(usernameInput);
            }

            // 验证密码
            if (!passwordInput.value) {
                showFieldError(passwordInput, '请输入密码');
                isValid = false;
            } else if (passwordInput.value.length < 6) {
                showFieldError(passwordInput, '密码长度不能少于6位');
                isValid = false;
            } else {
                clearFieldError(passwordInput);
            }

            return isValid;
        }

        function showFieldError(input, message) {
            input.classList.add('is-invalid');
            const errorElement = document.getElementById(input.id + 'Error');
            if (errorElement) {
                errorElement.textContent = message;
                errorElement.style.display = 'block';
            }
        }

        function clearFieldError(input) {
            input.classList.remove('is-invalid');
            const errorElement = document.getElementById(input.id + 'Error');
            if (errorElement) {
                errorElement.style.display = 'none';
            }
        }

        function showAlert(alertElement, message) {
            alertElement.textContent = message;
            alertElement.classList.remove('d-none');
        }

        function hideAlerts() {
            errorAlert.classList.add('d-none');
            successAlert.classList.add('d-none');
        }

        function setLoading(loading) {
            if (loading) {
                loginBtn.disabled = true;
                loginText.classList.add('d-none');
                loginSpinner.classList.remove('d-none');
            } else {
                loginBtn.disabled = false;
                loginText.classList.remove('d-none');
                loginSpinner.classList.add('d-none');
            }
        }

        // 清除验证状态
        [usernameInput, passwordInput].forEach(input => {
            input.addEventListener('input', function() {
                if (this.classList.contains('is-invalid')) {
                    clearFieldError(this);
                }
            });
        });
    });
</script>
</body>
</html>